package com.company.managesystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.company.managesystem.R;
import com.company.managesystem.adapter.ListViewAdapter;
import com.company.managesystem.common.Apis;
import com.company.managesystem.common.DataFetch;
import com.company.managesystem.model.WrokOrderBean;
import com.company.managesystem.model.WrokOrderItem;
import com.company.managesystem.util.SharePreferenceUtil;
import com.company.managesystem.util.ToastUtil;
import com.company.managesystem.viewholder.ListViewHolder;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * 未完成、正在维修、已完成、已评价公共列表
 * Created by lqy on 2017/8/21.
 */

public class CommonListActivity extends BaseActivity {

    private static final String TAG = "CommonListActivity";
    private GridView gridView;
    private RefreshLayout refreshLayout;
    private ListViewAdapter<WrokOrderBean> listViewAdapter;
    private String flag;
    // 设置翻页
    private int i = 1;
    private Intent intent;
    private Bundle bundle;

    @Override
    protected void onStart() {
        super.onStart();
        getData(true, 1);
    }

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_commonlist);
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        flag = intent.getStringExtra("flag");
        setToobarBackIcon(R.mipmap.ic_arrow_back);
        if (flag.equals("1")) {
            setToolBarTitle("未完成工单");
        } else if (flag.equals("2")) {
            setToolBarTitle("正在维修工单");
        } else if (flag.equals("3")) {
            setToolBarTitle("已完成工单");
        } else {
            setToolBarTitle("已评价工单");
        }
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        gridView = (GridView) findViewById(R.id.gridView);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));// 设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));// 设置footer
        refreshLayout.setDisableContentWhenRefresh(false);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(false);//是否在加载的时候禁止列表的操作
    }

    @Override
    protected void initData() {
        getData(true, i);
        listViewAdapter = new ListViewAdapter<WrokOrderBean>(R.layout.item_common_layout) {
            @Override
            protected void onBindData(ListViewHolder viewHolder, int position, final WrokOrderBean item) {
                if (flag.equals("1")) {
                    viewHolder.setText(R.id.common_button, "未完成");
                } else if (flag.equals("2")) {
                    viewHolder.setText(R.id.common_button, "正在维修");
                } else if (flag.equals("3")) {
                    viewHolder.setText(R.id.common_button, "已完成");
                } else {
                    viewHolder.setText(R.id.common_button, "已评价");
                }
                viewHolder.setText(R.id.common_date_text, item.getBx_Date());
                viewHolder.setText(R.id.common_order_text, item.getBx_Code());
                viewHolder.setText(R.id.common_time_text, item.getBx_Date());
                viewHolder.setText(R.id.common_department_text, item.getBxDw_Name());
                viewHolder.setText(R.id.common_content_text, item.getBx_Nr());
                viewHolder.setOnClickListener(R.id.common_button, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bundle = new Bundle();
                        if (flag.equals("1")) {
                            intent = new Intent(CommonListActivity.this, UnfinishedActivity.class);
                            bundle.putString("status", "unfinished");
                            bundle.putString("unfinished_code", item.getBx_Code());
                            bundle.putString("unfinished_time", item.getBx_Date());
                            bundle.putString("unfinished_address", item.getBxDw_Name());
                            bundle.putString("unfinished_content", item.getBx_Nr());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else if (flag.equals("2")) {
                            intent = new Intent(CommonListActivity.this, UnfinishedActivity.class);
                            bundle.putString("status", "repairing");
                            bundle.putString("repairing_code", item.getBx_Code());
                            bundle.putString("repairing_time", item.getBx_Date());
                            bundle.putString("repairing_address", item.getBxDw_Name());
                            bundle.putString("repairing_content", item.getBx_Nr());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else if (flag.equals("3")) {
                            intent = new Intent(CommonListActivity.this, FinishedActivity.class);
                            bundle.putString("status", "finished");
                            bundle.putString("finished_code", item.getBx_Code());
                            bundle.putString("finished_content", item.getBx_Nr());
                            bundle.putString("finished_address", item.getBx_Dd());
                            bundle.putString("finished_bxtime", item.getBx_Date());
                            bundle.putString("finished_department", item.getBxDw_Name());
                            bundle.putString("finished_bxname", item.getBxRy_Name());
                            bundle.putString("finished_jdtime", item.getJd_Date());
                            bundle.putString("finished_wctime", item.getWc_Date());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else {
                            Toast.makeText(CommonListActivity.this, "暂未开放！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        };
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getData(true, 1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                i += 1;
                getData(true, i);
            }
        });
    }

    private void getData(final boolean isRefresh, int page) {
        String url, jsrCode;
        jsrCode = new SharePreferenceUtil(this).getJsrCode();
        if (flag.equals("1")) {
            url = Apis.api_workorder + "?state=1015&page=" + page + "&jsrCode=" + jsrCode;
        } else if (flag.equals("2")) {
            url = Apis.api_workorder + "?state=1020&page=" + page + "&jsrCode=" + jsrCode;
        } else if (flag.equals("3")) {
            url = Apis.api_workorder + "?state=1030&page=" + page + "&jsrCode=" + jsrCode;
        } else {
            url = Apis.api_workorder + "?state=1040&page=" + page + "&jsrCode=" + jsrCode;
        }
        DataFetch dataFetch = new DataFetch<WrokOrderItem>(this, url, false, WrokOrderItem.class) {
            @Override
            protected void onDataLoaded(final WrokOrderItem data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (data.getState() == 1) {
                            if (isRefresh) {
                                listViewAdapter.clear();
                            }
                            listViewAdapter.addItems(data.getTable());
                            gridView.setAdapter(listViewAdapter);
                            refreshLayout.finishRefresh();
                        } else {
                            refreshLayout.finishLoadmore();
                            refreshLayout.finishRefresh();
                        }
                    }
                });
            }

            @Override
            protected void onDataLoadError() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(CommonListActivity.this, "网络错误，请重新加载！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        dataFetch.LoadingData(isRefresh);
    }
}
